<?php

namespace app\admin\model;

use think\Model;

/**
 * 上门服务
 * @package app\admin\model
 */
class Service extends Model
{
    # 设置当前模型对应的完整数据表名称
    protected $name = 'home_service';

    /**
     * 上门服务统计
     * @param $where
     * @return mixed
     */
    public static function countService($where = [])
    {
        return self::where($where)->count();
    }

    /**
     * 上门服务列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return mixed
     */
    public static function getServiceList($where, $page, $pagesize)
    {
        $list = self::alias('s')
            ->join('users u', 's.uid = u.id', 'left')
            ->where($where)
            ->field('s.*,u.nickname,u.headimg')
            ->order('s.create_time', 'desc')
            ->page($page, $pagesize)
            ->select();

        return $list ? $list->toArray() : [];
    }

    /**
     * 上门服务列表统计
     * @param $where
     * @return mixed
     */
    public static function countServiceList($where)
    {
        return self::alias('s')
            ->join('users u', 's.uid = u.id', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 单上门服务
     * @param $where
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findService($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 更新上门服务
     * @param $data
     * @param $where
     * @return Service
     */
    public static function updateService($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 服务地区分布
     * @return mixed
     */
    public static function getServiceArea()
    {
        $data = self::fieldRaw("COUNT(*) nums,s.city,l.name")
            ->alias('s')
            ->join('location l', 's.city = l.location_id', 'left')
            ->where('s.status', '<>', 3)
            ->group('s.city')
            ->order('nums', 'desc')
            ->limit(6)
            ->select();

        return $data ? $data->toArray() : [];
    }

}